package o;

import android.annotation.TargetApi;
import android.content.Context;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.HandlerThread;
import android.os.Message;
import android.view.Choreographer;
import android.view.Choreographer.FrameCallback;
import android.view.Display;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import com.huawei.hms.support.api.entity.hwid.HwIDConstant.ReqAccessTokenParm;

@TargetApi(16)
public final class anx {
    private long ʻ;
    private boolean ʼ;
    private long ʽ;
    private long ˊ;
    private final WindowManager ˋ;
    private long ˎ;
    private final e ˏ;
    private long ˏॱ;
    private long ͺ;
    private final d ॱ;
    private long ॱॱ;
    private long ᐝ;

    static final class d implements FrameCallback, Callback {
        private static final d ˎ = new d();
        private int ʽ;
        private final HandlerThread ˊ = new HandlerThread("ChoreographerOwner:Handler");
        private Choreographer ˋ;
        public volatile long ˏ = -9223372036854775807L;
        private final Handler ॱ;

        public static d ˊ() {
            return ˎ;
        }

        private d() {
            this.ˊ.start();
            this.ॱ = anr.ˏ(this.ˊ.getLooper(), (Callback) this);
            this.ॱ.sendEmptyMessage(0);
        }

        public void ˏ() {
            this.ॱ.sendEmptyMessage(1);
        }

        public void ˎ() {
            this.ॱ.sendEmptyMessage(2);
        }

        public void doFrame(long j) {
            this.ˏ = j;
            this.ˋ.postFrameCallbackDelayed(this, 500);
        }

        public boolean handleMessage(Message message) {
            switch (message.what) {
                case 0:
                    ˋ();
                    return true;
                case 1:
                    ॱ();
                    return true;
                case 2:
                    ʼ();
                    return true;
                default:
                    return false;
            }
        }

        private void ˋ() {
            this.ˋ = Choreographer.getInstance();
        }

        private void ॱ() {
            this.ʽ++;
            if (this.ʽ == 1) {
                this.ˋ.postFrameCallback(this);
            }
        }

        private void ʼ() {
            this.ʽ--;
            if (this.ʽ == 0) {
                this.ˋ.removeFrameCallback(this);
                this.ˏ = -9223372036854775807L;
            }
        }
    }

    @TargetApi(17)
    final class e implements DisplayListener {
        private final DisplayManager ˋ;
        final /* synthetic */ anx ॱ;

        public e(anx o_anx, DisplayManager displayManager) {
            this.ॱ = o_anx;
            this.ˋ = displayManager;
        }

        public void ˎ() {
            this.ˋ.registerDisplayListener(this, null);
        }

        public void ˊ() {
            this.ˋ.unregisterDisplayListener(this);
        }

        public void onDisplayAdded(int i) {
        }

        public void onDisplayRemoved(int i) {
        }

        public void onDisplayChanged(int i) {
            if (i == 0) {
                this.ॱ.ˋ();
            }
        }
    }

    public anx() {
        this(null);
    }

    public anx(@Nullable Context context) {
        if (context != null) {
            context = context.getApplicationContext();
            this.ˋ = (WindowManager) context.getSystemService("window");
        } else {
            this.ˋ = null;
        }
        if (this.ˋ != null) {
            e ˊ;
            if (anr.ˋ >= 17) {
                ˊ = ˊ(context);
            } else {
                ˊ = null;
            }
            this.ˏ = ˊ;
            this.ॱ = d.ˊ();
        } else {
            this.ˏ = null;
            this.ॱ = null;
        }
        this.ˎ = -9223372036854775807L;
        this.ˊ = -9223372036854775807L;
    }

    public void ˊ() {
        this.ʼ = false;
        if (this.ˋ != null) {
            this.ॱ.ˏ();
            if (this.ˏ != null) {
                this.ˏ.ˎ();
            }
            ˋ();
        }
    }

    public void ˏ() {
        if (this.ˋ != null) {
            if (this.ˏ != null) {
                this.ˏ.ˊ();
            }
            this.ॱ.ˎ();
        }
    }

    public long ˊ(long j, long j2) {
        long j3;
        long j4;
        long j5 = j * 1000;
        if (this.ʼ) {
            if (j != this.ʻ) {
                this.ͺ++;
                this.ʽ = this.ᐝ;
            }
            if (this.ͺ >= 6) {
                j3 = this.ʽ + ((j5 - this.ˏॱ) / this.ͺ);
                if (ˏ(j3, j2)) {
                    this.ʼ = false;
                    j4 = j2;
                    j3 = j5;
                } else {
                    j4 = (this.ॱॱ + j3) - this.ˏॱ;
                }
                if (!this.ʼ) {
                    this.ˏॱ = j5;
                    this.ॱॱ = j2;
                    this.ͺ = 0;
                    this.ʼ = true;
                }
                this.ʻ = j;
                this.ᐝ = j3;
                if (this.ॱ == null && this.ˎ != -9223372036854775807L) {
                    j3 = this.ॱ.ˏ;
                    return j3 != -9223372036854775807L ? ॱ(j4, j3, this.ˎ) - this.ˊ : j4;
                }
            } else if (ˏ(j5, j2)) {
                this.ʼ = false;
            }
        }
        j4 = j2;
        j3 = j5;
        if (this.ʼ) {
            this.ˏॱ = j5;
            this.ॱॱ = j2;
            this.ͺ = 0;
            this.ʼ = true;
        }
        this.ʻ = j;
        this.ᐝ = j3;
        return this.ॱ == null ? j4 : j4;
    }

    @TargetApi(17)
    private e ˊ(Context context) {
        DisplayManager displayManager = (DisplayManager) context.getSystemService(ReqAccessTokenParm.DISPLAY_LABEL);
        return displayManager == null ? null : new e(this, displayManager);
    }

    private void ˋ() {
        Display defaultDisplay = this.ˋ.getDefaultDisplay();
        if (defaultDisplay != null) {
            this.ˎ = (long) (1.0E9d / ((double) defaultDisplay.getRefreshRate()));
            this.ˊ = (this.ˎ * 80) / 100;
        }
    }

    private boolean ˏ(long j, long j2) {
        return Math.abs((j2 - this.ॱॱ) - (j - this.ˏॱ)) > 20000000;
    }

    private static long ॱ(long j, long j2, long j3) {
        long j4;
        long j5 = (((j - j2) / j3) * j3) + j2;
        if (j <= j5) {
            j4 = j5 - j3;
        } else {
            j4 = j5;
            j5 += j3;
        }
        if (j5 - j < j - j4) {
            return j5;
        }
        return j4;
    }
}
